<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate"
xmlns:util="http://exist-db.org/xquery/util">

	<p:param type="output" name="data"/>

	<p:processor name="oxf:request">
		<p:input name="config">
			<config >
				<include>/request/parameters</include>
			</config>
		</p:input>
		<p:output name="data" id="request"/>
	</p:processor>

	<p:processor name="oxf:xslt">
		<p:input name="data" href="#request#xpointer(/request/parameters)"/>
		<p:output name="data" id="control"/>
		<p:input name="config">
			<xdb:query xsl:version="2.0" collection="/db/portal">
				xquery version "1.0";
				<xsl:variable name="username" select="/parameters/parameter[name='username']/value"/>
				<xsl:variable name="password" select="/parameters/parameter[name='password']/value"/>
				<xsl:choose>
					<xsl:when test="$username = ''">
						<error>Please provide a username.</error>
					</xsl:when>
					<xsl:when test="not(exists(/parameters/parameter/name[. = 'userId'])) and string-length($password) &lt; 8">
						<error>The new password you provided is too short and must be at least composed of 8 characters.</error>
					</xsl:when>
					<xsl:when test="string-length($password) &lt; 8 and exists(/parameters/parameter/name[. ='userId']) and $password != ''">
						<error>The new password you provided is too short and must be at least composed of 8 characters.</error>
					</xsl:when>
					<xsl:when test="/parameters/parameter[name='username']/value != ''">
				if(exists(/users/user[@id='<xsl:value-of select="/parameters/parameter[name='username']/value"/>']) and '<xsl:value-of select="/parameters/parameter[name='username']/value"/>' != '<xsl:value-of select="/parameters/parameter[name='userId']/value"/>') then <error>Username already in use.</error> else <message/>
					</xsl:when>
					<xsl:otherwise>
						<message/>
					</xsl:otherwise>
				</xsl:choose>
			</xdb:query>
		</p:input>
	</p:processor>
	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#control"/>
		<p:output name="data" id="valid"/>
	</p:processor>

	<!--Test validity-->
	<p:choose href="#valid">
		<p:when test="exists(/error)">
			<p:processor name="oxf:identity">
				<p:input name="data" href="#valid"/>
				<p:output name="data" id="message"/>
			</p:processor>
		</p:when>
		<p:otherwise>

			<!--Edit or create-->
			<p:choose href="#request">
				<p:when test="exists(/request/parameters/parameter[name='userId'])">
					<p:processor name="oxf:xslt">
						<p:input name="data" href="#request#xpointer(/request/parameters)"/>
						<p:output name="data" id="query"/>
						<p:input name="config">
							<xsl:stylesheet version="2.0">
								<xsl:template match="/">
									<xdb:query collection="/db/portal">
xquery version "1.0";
let $user := /users/user[@id='<xsl:value-of select="/parameters/parameter[name='userId']/value"/>'] return (
										<xsl:apply-templates select="/parameters/parameter[name!='user_pref' and value != '' and name != 'userId']"/>
,<message>The user has been successfuly updated.</message>)
									</xdb:query>
								</xsl:template>
								<xsl:template match="parameter">

update replace $user/<xsl:value-of select="name"/> with
element <xsl:value-of select="name"/> {<xsl:if test="name != 'password'">"<xsl:value-of select="value"/>"</xsl:if>
									<xsl:if test="name = 'password'">util:md5(string('<xsl:value-of select="value"/>'))
									</xsl:if>}<xsl:if test="name = 'username'">, update replace $user/@id with "<xsl:value-of select="value"/>"</xsl:if>
									<xsl:if test="position() != last()">,</xsl:if>
								</xsl:template>
							</xsl:stylesheet>
						</p:input>
					</p:processor>

				</p:when>
				<p:otherwise>
					<p:processor name="oxf:xslt">
						<p:input name="data" href="#request#xpointer(/request/parameters)"/>
						<p:output name="data" id="query"/>
						<p:input name="config">
							<xsl:stylesheet version="2.0">
								<xsl:template match="/">
									<xdb:query collection="/db/portal">
xquery version "1.0";
(update insert <user id="{/parameters/parameter[name='username']/value}">
											<username>
												<xsl:value-of select="/parameters/parameter[name='username']/value"/>
											</username>
											<password>{util:md5(string('<xsl:value-of select="/parameters/parameter[name='password']/value"/>'))}</password>
											<copname/>
											<level>
												<xsl:value-of select="/parameters/parameter[name='level']/value"/>
											</level>
											<lang>
												<xsl:value-of select="/parameters/parameter[name='lang']/value"/>
											</lang>
											<interface/>
										</user> into /users,<message>The user has been successfuly created.</message>)
									</xdb:query>
								</xsl:template>
							</xsl:stylesheet>
						</p:input>
					</p:processor>
				</p:otherwise>
			</p:choose>
			<p:processor name="oxf:xmldb-query">
				<p:input name="datasource" href="/config/datasource.xml"/>
				<p:input name="query" href="#query"/>
				<p:output name="data" id="message"/>
			</p:processor>
		</p:otherwise>
	</p:choose>


	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../users.xpl"/>
		<p:output name="data" id="datas"/>
	</p:processor>

	<p:processor name="oxf:identity">
		<p:input name="data" href="aggregate('data', #message, #datas#xpointer(/data/*))"/>
		<p:output name="data" ref="data"/>
	</p:processor>


</p:config>